#ifndef NP_DAOBASE_H
#define NP_DAOBASE_H

#include <string>
#include <mysql/mysql.h>

#define TRAVERSE_AND_RETURN_ROWS(BUFFER) \
  MYSQL_RES* res = doQuery(sql); \
  int toReturn = traverseResult(res, BUFFER); \
  mysql_free_result(res); \
  return toReturn

class Daobase
{
protected:
  Daobase() : m_conn(NULL) {}
  Daobase(const MYSQL* conn);
  virtual ~Daobase() {}

  void doDml(const std::string& dml);
  MYSQL_RES* doQuery(const std::string& query);

  void destoryResult(MYSQL_RES* result);


private:
  const char* getErrorMsg(const std::string& sql);

public:
  void setConn(const MYSQL* conn) { m_conn = const_cast<MYSQL*>(conn); }

protected:
  MYSQL* m_conn;
};

#endif // NP_DAOBASE_H
